if (typeof(own) === 'undefined') {
    var own = {};
}
own.nav = {
    _init: function () {
        var obj = $All('.own-nav');
        for (var i = 0, cnt = obj.length; i < cnt; i++) {
            own.nav.load({
                ele: obj[i],
                filter: obj[i].attributes[own.config.filterName].nodeValue,
                type:(obj[i].className.indexOf('own-nav-horizontal') >= 0?'horizontal':'vertical')
            });
        }
    },
    /**
     * 加载nav数据信息
     * @param opt
     */
    load: function (opt) {
        var def = {
            ele: null,
            filter: null,
            temp: '<i class="own-nav-ico-down iconfont icon-caret-down" ></i>',
            width: null,
            height: null,
            fun: null,
            /**
             * 只能打开一个层级
             */
            isOneTier:true,
            /**
             * 定义展开事件click表示点击才打开，mouseleave进入才展开
             */
            action:'',
            /**
             * nav的类型，是横向还是竖向，vertical竖向horizontal横向
             */
            type:'vertical'
        }

        def = own.util.copy(def, opt);

        if(own.util.isEmpty(def.action)){
            if(def.type == 'vertical'){
                def.action = 'click';
            }else{
                def.action = 'mouseover';
            }
        }

        var child = [def.ele.querySelectorAll('ul')[0]];
        try{
            loadNode(child);
        }catch (e){}
        function loadNode(child) {
            for (var i = 0, cnt = child.length; i < cnt; i++) {
                var ch = child[i].querySelectorAll('.own-nav-li');
                for (var j = 0, cnt1 = ch.length; j < cnt1; j++) {
                    (function(j,ch){
                        var a = ch[j].querySelector('a');
                        var islower = ch[j].querySelector('ul');//是否存在下级,存在下级会返回下级对象
                        var down = own.util.parseDom(def.temp)[0];
                        if (islower) {//有下级，直接插入下拉按钮
                            a.appendChild(down);
                        }
                        //进行判断操作是否需要添加对应的
                        a['on'+def.action] = function (event) {
                            return changeNode(event,j,ch,islower,down);
                        }
                    })(j,ch);
                }
            }
        }

        function changeNode(event,j,ch,islower,down){
            var event=event||window.event;//兼容IE
            if(event.preventDefault) {    //标准技术
                event.preventDefault();
            }
            if(event.returnValue) {
                event.returnValue = false;
            }
            if(event.stopPropagation) {
                event.stopPropagation();
            }

            if(def.isOneTier){
                //需要关闭同级其他的窗口
                var node = own.util.getChildNodes(ch[j].parentNode);
                for(var ii=0,cnt2=node.length;ii<cnt2;ii++){
                    if(node[ii] != ch[j]&&node[ii].attributes['isopen']){
                        if(node[ii].querySelector('a') != null){
                            node[ii].querySelector('a')['on'+def.action]();
                        }
                    }
                }
            }

            //只做通知，不做具体操作
            var truth_ = true;
            if (def.fun != null) {
                truth_ = def.fun(e,function(){
                    clickNode(ch[j],islower,down);
                });
            } else {
                truth_ = own.event.execute(def.filter, 'click', ch[j], islower,function(){
                    clickNode(ch[j],islower,down);
                });
            }
            truth_ = truth_ == undefined?true:truth_;
            if(truth_){
                clickNode(ch[j],islower,down);
            }
            return false;
        }

        /**
         * 点击事件处理
         * @param obj
         */
        function clickNode(obj,islower,down){

            if(islower){//有下级
                var isanim = obj.attributes['isanim'];
                isanim = isanim == undefined?false:isanim;
                if(isanim){
                    return;
                }
                obj.attributes['isanim'] = true;
                var isopen = obj.attributes['isopen'];
                isopen = isopen==undefined?false:isopen;

                if(!isopen){
                    Velocity(down,{
                        rotateZ: '180deg'
                    }, {
                        easing: "ease-in-out",
                        complete:function(){
                            obj.attributes['isanim'] = false;
                            obj.attributes['isopen'] = true;
                        }
                    });
                    Velocity(islower,"slideDown", {
                        easing: [320, 13]
                    });
                }else{
                    Velocity(down,'reverse',{
                        easing: "ease-in-out",
                        complete:function(){
                            obj.attributes['isanim'] = false;
                            obj.attributes['isopen'] = false;
                        }
                    });
                    Velocity(islower,"slideUp", {
                        easing: "ease-in-out"
                    });
                }
            }else{//没有下级

            }
        }

    }
}